import json
from itertools import groupby

import requests


# 用户名密码认证(私密代理/独享代理)

# proxies = {
#     "http": "http://119.132.72.248:3328",
#     "https": "http://119.132.72.248:3328"
# }

proxies = {
    'http': 'http://120.66.666.666:11166',
    'https': 'http://120.66.666.666:11166'
}

# proxies = {
#     'http': '120.66.666.666:11166',
#     'https': '120.66.666.666:11166'
# }
# # 测试是否使用代理
target_url = "https://httpbin.org/ip"
#
# # 使用代理IP发送请求
# response = requests.get(target_url, proxies=proxies, timeout=(5, 5))
# if response.status_code == 200:
#     print(response.text)
# response = requests.get(target_url)
ip_list = []
time=0
for i in range(1, 1000):
    try:
        response = requests.get(
            target_url, proxies=proxies,  timeout=(5, 5))
        # 获取页面内容
        if response.status_code == 200:
            elapsed_time = response.elapsed.total_seconds()
            # data = json.loads(response.text)
            # origin_ip = data['origin']
            # ip_list.append(origin_ip)
            time+=elapsed_time
            data = json.loads(response.text)
            ip=data['origin']
            ip_list.append(ip)
            print(f'{ip} 响应时长：', elapsed_time, '秒')
            # print('https://www.baidu.com 响应时长：', elapsed_time, '秒')
            # print(response.content.decode('utf-8', errors='ignore')
    except Exception as e:
        print(e)
print(ip_list)
group_count = {}
for ip in ip_list:
    if ip in group_count:
        group_count[ip] += 1
    else:
        group_count[ip] = 1
group_count_sort = dict(sorted(group_count.items(), key=lambda item: item[1], reverse=True))
print(group_count_sort)
